﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-11 17:06:41
 * 功能：广告管理
**************************************************************************************/

using Game.Framework;
using System;

public class AdMgr : BaseManager<AdMgr>
{
    private Reward m_Reward;
    private Interstitial m_Int;
    private Open m_Open;
    private Banner m_Banner;
    // 是否可以显示开屏广告
    private bool m_IsCanShowOpenAd = true;

    private AdMgr()
    {
        EventCenter.Instance.AddEventListen<bool>(EventName.MonoOnApplicationPause, OnApplicationPause);
        EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_BE_ABOUT_TO_SHOW_AD, OnShowSuccessAd);
        EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_SUCCESS_AD, OnShowSuccessAd);
        EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_CLOSE_AD, OnCloseAd);
        EventCenter.Instance.AddEventListen<EAdType, ETriggerAdType>(GameEventName.ON_SHOW_FAILED_AD, OnCloseAd);
        EventCenter.Instance.AddEventListen<string>(GameEventName.REQUEST_BUY, (arg1) => OnShowSuccessAd(EAdType.Splash, ETriggerAdType.None));
        EventCenter.Instance.AddEventListen<string, object>(GameEventName.SUCC_BUY, (arg1, arg2) => OnCloseAd(EAdType.Splash, ETriggerAdType.None));
        EventCenter.Instance.AddEventListen<string>(GameEventName.BUY_FAILED, (arg1) => OnCloseAd(EAdType.Splash, ETriggerAdType.None));
    }

    private void OnShowSuccessAd(EAdType adType, ETriggerAdType triggerType)
    {
        m_IsCanShowOpenAd = false;
    }

    private void OnCloseAd(EAdType adType, ETriggerAdType triggerType)
    {
        m_IsCanShowOpenAd = true;
    }

    private void OnApplicationPause(bool pauseStatus)
    {
        LogManager.Log($"恢复应用处理， {pauseStatus}  {BaseDataMgr.Instance.Cfg.ads.open_h == 1}  {m_IsCanShowOpenAd}", "广告");
        if (!pauseStatus && GameDataMgr.Instance.CurLevel > BaseDataMgr.Instance.Cfg.ads.open_h && m_IsCanShowOpenAd && !GameDataMgr.Instance.IsPurchased_RemoveAds())
        {
            LogManager.Log("恢复应用处理，展示开屏广告", "广告");
            ShowOpen(ETriggerAdType.AppOpen);
        }
    }

    public void Init()
    {
        var cfg = BaseDataMgr.Instance.Cfg.ads;
        m_Reward = new Reward(cfg.reward[0]);
        m_Int = new Interstitial(cfg.interstitial[0]);
        m_Open = new Open(cfg.open[0]);
        if (cfg.banner != null && cfg.banner.Length > 0) m_Banner = new Banner(cfg.banner[0]);

        MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration sdkConfiguration) =>
        {
            m_Reward.Initialize();
            m_Int.Initialize();
            m_Open.Initialize();
            if (m_Banner != null) m_Banner.Initialize();
            LogManager.Log("AppLovin SDK is initialized, start loading ads", "广告");
        };

        MaxSdk.InitializeSdk();
    }

    public void ShowReward(ETriggerAdType type)
    {
        if (m_Reward != null)
        {
            m_Reward.Show(type);
        }
    }

    public bool IsRewardReady() => m_Reward != null && m_Reward.IsReady();

    public void ShowInt(ETriggerAdType type)
    {
        if (m_Int != null)
        {
            m_Int.Show(type);
        }
    }

    public void ShowOpen(ETriggerAdType type)
    {
        if (m_Open != null)
        {
            m_Open.Show(type);
        }
    }

    public bool IsOpenReady() => m_Open != null && m_Open.IsReady();

    public void ShowBanner(ETriggerAdType type)
    {
        if (m_Banner != null)
        {
            m_Banner.Show(type);
        }
    }
}